package cn.txun.springboot33.controller;

import cn.txun.springboot33.entity.Item;
import cn.txun.springboot33.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {

    @Autowired
    private ItemMapper itemMapper;

    @RequestMapping("/insert")
    public String insert(Item item){
        System.out.println("item = " + item);
        itemMapper.insert(item);
        return "新增商品成功";
    }

    @RequestMapping("/select")
    public String select(){
        List<Item> list = itemMapper.select();
        String html = "<table border='1' >";
        html+="<tr><th>id</th><th>商品名称</th><th>价格</th><th>描述</th><th>操作</th></tr>";
        for(Item item:list){
            html+="<tr>";
            html+="<td>"+item.getId()+"</td>";
            html+="<td>"+item.getName()+"</td>";
            html+="<td>"+item.getPrice()+"</td>";
            html+="<td>"+item.getIntro()+"</td>";
            html+="<td><a href='/delete?id="+item.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        itemMapper.deleteById(id);
        return "删除成功";
    }

    @RequestMapping("/update")
    public String update(Item item){
        itemMapper.update(item);
        return "更新成功";
    }

}
